package sk.robocup.dvanastyhrac.run;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import robocup.component.RobocupAgent;
import sk.robocup.dvanastyhrac.config.ConfigUtil;
import soccerscope.SoccerScope;

/**
 * Tato trieda nerobi nic ine len extendne normalneho robocup agenta, v
 * pozmenenom maine naloaduje konfig v ktorom je testovaci stav pre komunikaciu
 * a pusti takehoto hraca.
 *
 * @author gpan
 */
public class Run12hrac extends RobocupAgent {

    private static ConfigUtil conf;

    public Run12hrac(Properties props) {
        super(props);
    }

    /**
     * Ziadne parametre, len si vytvorim hraca s prislusnym konfigom a spustim
     * Ak si zelame spustit hraca z prikazoveho riadku z jarka pouzijeme prikaz:
     * >java -cp lib;dvanastyhrac.jar sk.robocup.dvanastyhrac.run.Run12hrac
     * za tento prikaz mozeme dat prepinac -conf za ktory dame cestu ku konfiguracnemu suboru.
     * Ak sa cesta neuvedie, pouzije sa defaultny konfigurak z adresara resources.
     * Ak sa ani tam nenachadza, pouziju sa defaultne hodnoty priamo z kodu. vid. ConfigUtil
     * @param args argumenty main metody [-conf]
     */
    public static void main(String[] args) {

        // nacitavanie konfiguraku z parametrov
        // syntax "[RobocupAgent][MAIN] -conf [Cesta_ku_konfiguraku]"
        // PS: v ceste je nutne pouzit opacne lomitka '/'
        if (args.length == 2) {
            //ak bol zadany parameter
            if (args[0].contains("-conf")) {
                conf = ConfigUtil.get(args[1]); //nacitaj konfigurak
            } else {
                System.out.println("Ussage: [-conf \"path to conf\"]");
                return;
            }
        } else {
            conf = ConfigUtil.get(); //nacitaj implicitny
        }

        // nastavenie variables z konfigu

        String teamname = conf.getTEAM_NAME();
        String host = conf.getHOST();

        //chceme pustit gui?
        boolean connectgui = conf.isCONNECT_GUI();
        //chceme aby sa posielali veci z hraca do gui? ak ano hromadi sa to v pamati
        boolean record = conf.isRECORD();

        //Reading global properties
        System.out.println("Spustam hracov...");
        List<String> configs = new ArrayList<String>();
        configs.add(conf.getPLAYER_1());
        configs.add(conf.getPLAYER_2());
        configs.add(conf.getPLAYER_3());
        configs.add(conf.getPLAYER_4());
        configs.add(conf.getPLAYER_5());
        configs.add(conf.getPLAYER_6());
        configs.add(conf.getPLAYER_7());
        configs.add(conf.getPLAYER_8());
        configs.add(conf.getPLAYER_9());
        configs.add(conf.getPLAYER_10());
        configs.add(conf.getPLAYER_11());

        for (String config : configs) {
            Properties props = loadProperties(config);
            if (props != null) {

                // update gloabl props:
                props.put(TEAM_NAME, teamname);
                props.put(HOST_NAME, host);

                //tato premenna hovori ci sa ma pustit monitor spolu s hracom, ak sa pusti, hromadi sa pamat.
                if (connectgui) {
                    props.put(GUI_CONNECT, "true");
                } else {
                    props.put(GUI_CONNECT, "false");
                }

                if (connectgui && !record) {
                    props.put(GUI_CONNECT, "false");
                    //monitor si pustime nas, nenaviazeme hracov, nehromadi sa pamat
                    SoccerScope.getInstance();
                }


                System.out.println("Successfully loaded properties from " + config);
                Run12hrac agent = new Run12hrac(props);
                agent.initialize();
                agent.start();
            } else {
                System.out.println("Failed to load properties from " + config);
            }
        }
    }

    /**
     * Metoda nacita konfiguracne subory hracov.     *
     * @param fileName
     *            konfiguracny subor hraca
     * @return Properties - nacitane properties
     */
    public static Properties loadProperties(String fileName) {
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(fileName));
        } catch (Exception e) {
            InputStream stream = Run12hrac.class.getClassLoader().getResourceAsStream(fileName);
            if (stream == null) {
                System.err.println("Nie je mozne nacitat properties zo suboru: "+fileName);
                e.printStackTrace();
                return null;
            }
            try {
                p.load(stream);
            } catch (Exception e1) {
                e1.printStackTrace();
                return null;
            }
        }
        return p;
    }
}
